package weaver.tools.util;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author
 * @ClassName: BigDecimalCalculate
 * @Description: 加减乘除计算：字符串类型数字转BigDecimal
 * @date 2024-04-16
 */
public class CalBaseUtil {

    /**
     * @Description: 相加
     */
    public static String bAdd(String a, String b) {
        a = isZero(a);
        b = isZero(b);
        String result;
        try {
            BigDecimal bg1 = new BigDecimal(a);
            BigDecimal bg2 = new BigDecimal(b);
            BigDecimal bd = bg1.add(bg2);
            result = bd.toString();
        } catch (NumberFormatException e) {
            result = "";
        }
        return result;
    }

    /**
     * @Description: 相减
     */
    public static String bSubtract(String a, String b) {
        a = isZero(a);
        b = isZero(b);
        String result;
        try {
            BigDecimal bg1 = new BigDecimal(a);
            BigDecimal bg2 = new BigDecimal(b);
            BigDecimal bd = bg1.subtract(bg2);
            result = bd.toString();
        } catch (NumberFormatException e) {
            result = "";
        }
        return result;
    }

    /**
     * @Description: 相乘
     */
    public static String bMultiply(String a, String b) {
        a = isZero(a);
        b = isZero(b);
        String result;
        try {
            BigDecimal bg1 = new BigDecimal(a);
            BigDecimal bg2 = new BigDecimal(b);
            BigDecimal bd = bg1.multiply(bg2);
            result = bd.toString();
        } catch (NumberFormatException e) {
            result = "";
        }
        return result;
    }

    /**
     * @Description: 除法保留小数位，舍弃多余小数
     */
    public static String bDivide2(String a, String b, int n) {
        a = isZero(a);
        b = isZero(b);
        String result;
        try {
            BigDecimal bg1 = new BigDecimal(a);
            BigDecimal bg2 = new BigDecimal(b);
            BigDecimal bd = bg1.divide(bg2, n, RoundingMode.DOWN);
            result = bd.toString();
        } catch (NumberFormatException e) {
            result = "";
        }
        return result;
    }

    /**
     * @Description: 除法保留小数位(四舍五入)
     */
    public static String bDivide(String a, String b, int n) {
        a = isZero(a);
        b = isZero(b);
        String result;
        try {
            BigDecimal bg1 = new BigDecimal(a);
            BigDecimal bg2 = new BigDecimal(b);
            BigDecimal bd = bg1.divide(bg2, n, RoundingMode.HALF_UP);
            result = bd.toString();
        } catch (NumberFormatException e) {
            result = "";
        }
        return result;
    }

    /**
     * @Description: 乘法保留小数位(四舍五入)
     */
    public static String bMultiply(String a, String b, int n) {
        a = isZero(a);
        b = isZero(b);
        String result;
        try {
            BigDecimal bg1 = new BigDecimal(a);
            BigDecimal bg2 = new BigDecimal(b);
            BigDecimal bd = bg1.multiply(bg2).setScale(n, RoundingMode.HALF_UP);
            result = bd.toString();
        } catch (NumberFormatException e) {
            result = "";
        }
        return result;
    }

    /**
     * @Description: 乘法保留小数位(四舍五入)
     */
    public static String bMultiply2(String a, String b, int n) {
        a = isZero(a);
        b = isZero(b);
        String result;
        try {
            BigDecimal bg1 = new BigDecimal(a);
            BigDecimal bg2 = new BigDecimal(b);
            BigDecimal bd = bg1.multiply(bg2).setScale(n, RoundingMode.DOWN);
            result = bd.toString();
        } catch (NumberFormatException e) {
            result = "";
        }
        return result;
    }

    /**
     * @Description: 大小比较 -1 0 1
     */
    public static int bCompare(String a, String b) {
        a = isZero(a);
        b = isZero(b);
        int result;
        try {
            BigDecimal bg1 = new BigDecimal(a);
            BigDecimal bg2 = new BigDecimal(b);
            result = bg1.compareTo(bg2);
        } catch (NumberFormatException e) {
            result = -1;
        }
        return result;
    }

    /**
     * @Description: 取绝对值
     */
    public static String babs(String a) {
        a = isZero(a);
        String result;
        try {
            BigDecimal bg1 = new BigDecimal(a);
            result = bg1.abs().toString();
        } catch (NumberFormatException e) {
            result = "";
        }
        return result;
    }

    /**
     * @Description: 取模值
     */
    public static String bmodel(String a, String b) {
        String result;
        try {
            BigDecimal bg1 = new BigDecimal(a);
            BigDecimal bg2 = new BigDecimal(b);
            result = bg1.divideAndRemainder(bg2).toString();
        } catch (NumberFormatException e) {
            result = "";
        }
        return result;
    }

    /**
     * @Description: 为零判断
     */
    public static String isZero(String a) {
        if ("".equals(a)) {
            a = "0";
        }
        return a;
    }

    /**
     * @Description: 自动生成编号
     */
    private static String autoGenericCode(String code) {
        String result = "";
        // 保留code的位数
        result = String.format("%0" + code.length() + "d", Integer.parseInt(code) + 1);
        return result;
    }

    /**
     * @Description: 自动生成编号保留位数
     */
    private static String autoGenericCode(String code, int num) {
        String result = "";
        result = String.format("%0" + num + "d", Integer.parseInt(code) + 1);
        return result;
    }

    /**
     * @Description: 判断是否是数字
     */
    public static String isNumberType(String str) {
        boolean strResult = str.matches("-?[0-9]+.?[0-9]*");
        return strResult ? str : "0";
    }

    /**
     * @Description: 截取数字
     */
    public static String getNumbers(String content) {
        Pattern NUMBER_PATTERN = Pattern.compile("[0-9]+");
        Matcher matcher = NUMBER_PATTERN.matcher(content);
        while (matcher.find()) {
            return matcher.group(0);
        }
        return "";
    }

    public static void main(String[] args) {
        System.out.println(bAdd("123", "ss"));
    }
}
